using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyDoanVien.General;
using QuanLyDoanVien.DataLayer;
using QuanLyDoanVien.Bussiness;
using QuanLyDoanVien.Controller;

namespace QuanLyDoanVien
{
    public partial class frmQLCV : DevComponents.DotNetBar.Office2007Form
    {
        ChucVuCtrl m_ChucVuCtrl = new ChucVuCtrl();
        List<ChucVuInfo> dscv = new List<ChucVuInfo>();
        public frmQLCV()
        {
            InitializeComponent();
            DataService.OpenConnection();
        }
        private void Refesh_ChucVu()
        {
            this.txt_CV_Ma.Text = "";
            this.txt_CV_Ten.Text = "";
        }
        private void Load_DS_ChucVu()
        {
            dataGridView_CV.Rows.Clear();
            Refesh_ChucVu();
            dscv = m_ChucVuCtrl.Lay_DS_ChucVu();
            foreach (ChucVuInfo cv in dscv)
            {
                dataGridView_CV.Rows.Add(cv.MaChucVu,cv.TenChucVu);
            }
            try
            {
                frmQLHS.cbx_ChucVu.DataSource = dscv;
                frmQLHS.cbx_ChucVu.ValueMember = "MaChucVu";
                frmQLHS.cbx_ChucVu.DisplayMember = "TenChucVu";
            }
            catch { }

        }

        private void btn_CV_Them_Click(object sender, EventArgs e)
        {
            String id = m_ChucVuCtrl.getAutoId();
            ChucVuInfo cv = new ChucVuInfo(id,this.txt_CV_Ten.Text);
            m_ChucVuCtrl.ThemChucVu(cv);
            Load_DS_ChucVu();
        }

        private void frmQLCV_Load(object sender, EventArgs e)
        {
            Load_DS_ChucVu();
        }

        private void btn_CV_Xoa_Click(object sender, EventArgs e)
        {
            m_ChucVuCtrl.XoaChucVu(txt_CV_Ma.Text);
            Load_DS_ChucVu();
        }

        private void dataGridView_CV_SelectionChanged(object sender, EventArgs e)
        {
            int index = -1;
            if (dataGridView_CV.SelectedRows.Count <= 0 || dataGridView_CV.SelectedRows[0].Index == dataGridView_CV.RowCount - 1)
            {
                return;
            }
            else
            {
                index = dataGridView_CV.SelectedRows[0].Index;
            }
            // dataGridView_L.DefaultCellStyle.SelectionBackColor = Color.Red;
            DataGridViewRow dr = dataGridView_CV.Rows[index];
            this.txt_CV_Ma.Text = dr.Cells["MaChucVu"].Value.ToString();
            this.txt_CV_Ten.Text = dr.Cells["TenChucVu"].Value.ToString();
        }

        private void btn_CV_LamMoi_Click(object sender, EventArgs e)
        {
            Load_DS_ChucVu();
            this.Refesh_ChucVu();
            this.txt_CV_Ma.Text = m_ChucVuCtrl.getAutoId();
            

        }

        private void btn_CV_Luu_Click(object sender, EventArgs e)
        {
            ChucVuInfo cv = new ChucVuInfo(txt_CV_Ma.Text,txt_CV_Ten.Text);
            m_ChucVuCtrl.SuaChucVu(cv);
            Load_DS_ChucVu();
        }

        
    }
}